

-- Lightroom SDK
local LrBinding = import 'LrBinding'
local LrDialogs = import 'LrDialogs'
local LrFileUtils = import 'LrFileUtils'
local LrPathUtils = import 'LrPathUtils'
local LrView = import 'LrView'

local logger = import 'LrLogger'( 'RhubarbExportService' )

local bind = LrView.bind
local share = LrView.share

-- Rhubarb plugin
require('RhubarbHttpClient')
require('RhubarbExportDialogSections')


local exportServiceProvider = {}


exportServiceProvider.exportPresetFields = {
	{ key = 'gallery_url', default = 'http://www.mydomain/mygallery/'},
	{ key = 'auth_token', default = '' },
	{ key = 'album_name', default = '' },
	{ key = 'album_pass', default = '' },
}

--exportServiceProvider.showSections = { 'fileNaming', 'fileSettings', etc... } -- not used for Flickr plug-in
exportServiceProvider.hideSections = { 'exportLocation' }
-- exportServiceProvider.canExportToTemporaryLocation = true -- not used for Flickr plug-in
exportServiceProvider.allowFileFormats = { 'JPEG' }
exportServiceProvider.hidePrintResolution = true
exportServiceProvider.canExportVideo = false

exportServiceProvider.sectionsForTopOfDialog = RhubarbExportDialogSections.sectionsForTopOfDialog

function exportServiceProvider.processRenderedPhotos( functionContext, exportContext )

	local exportSession = exportContext.exportSession

	-- local reference to the export parameters
	
	local exportSettings = assert( exportContext.propertyTable )
	
--	LrDialogs.message( exportSettings.gallery_url )
		
	-- request total number of photos
	
	local nPhotos = exportSession:countRenditions()
	
	-- Set progress title.
	
	local progressScope = exportContext:configureProgress {
						title = nPhotos > 1
									and LOC( "$$$/Rhubarb/Publish/Progress=Publishing ^1 photos to rhubarb", nPhotos )
									or LOC "$$$/Rhubarb/Publish/Progress/One=Publishing one photo to rhubarb",
						}
						
	-- Iterate over all photos
	
	for i, rendition in exportContext:renditions { stopIfCanceled = true } do
	
		-- update the progress information 
		
		progressScope:setPortionComplete( ( i - 1 ) / nPhotos )
		
		-- Get next photo.

		local photo = rendition.photo
	
		if not rendition.wasSkipped then

			local success, pathOrMessage = rendition:waitForRender()
			
			-- Update progress scope again once we've got rendered photo.
			
			progressScope:setPortionComplete( ( i - 0.5 ) / nPhotos )
			
			-- Check for cancellation again after photo has been rendered.
			
			if progressScope:isCanceled() then break end
			
			if success then
			
				local server = exportSettings.gallery_url
				local authToken = exportSettings.auth_token

				local album_name = exportSettings.album_name
				local album_pass = exportSettings.album_pass
				
				logger:info( 'file should be uploaded:', pathOrMessage )

				local timoutSeconds=300
				RhubarbHttpClient.uploadImage(server, authToken, album_name, album_pass, pathOrMessage, timeoutSeconds)

				-- delete the temporary photo file. 
					
				LrFileUtils.delete( pathOrMessage )
				
			end
		end
	end --end loop over all images

	progressScope:done()

end

return exportServiceProvider
